import {
  getVerifyCode
} from '@/api/login/login'
import {
  GetBaseInfo
} from '@/api/base'

import Form from './Form/Form'
import { setHasToken, setLogin, setIsLogin } from '@/utils/auth'
import ServiceForm from './serviceAdr/index.vue'
import df from '@/utils/df'
export default {
  name: "Login",
  components: {
    ServiceForm,
    Form
  },
  data: function () {
    return {
      loginForm: {
        jkydm: "",
        jkymm: "",
        yzm: ""
      },
      baseInfo: {
        ksmc: '',
        kdmc: '',
        tdmc: '',
      },
      loginRules: {
        jkydm: [
          {
            required: true,
            trigger: "blur",
            message: '请输入监考员代码',
          },
        ],
        jkymm: [
          {
            required: true,
            trigger: "blur",
            message: '请输入监考员密码',
          },
        ],
        yzm: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("login.loginCodeTip"),
          },
        ],
      },
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      imgCode: '',
      formVisible: false,
      serviceFormVisible: false
    };
  },
  computed: {
    loginLoading() {
      return this.$store.state.user.loginLoading;
    },
  },
  watch: {
    loginLoading(val) {
      if (!val) this.loading = false;
    },
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
        }
      },
      immediate: true,
    },
  },
  created() {
    const _this = this;
  },
  mounted() {
    this.getToken()
  },

  methods: {
    initData () {
      this.getToken()
    },
    getToken () {
      let that = this
      that.$store
        .dispatch("user/token", {app_key: 'Lab', app_secret: '0731400621'})
        .then(() => {
          that.getVerifyCodeImg()
          that.getBaseInfo()
        })
        .catch(() => {
          that.$store.commit("user/SET_LOGIN_LOADING", false);
        });
    },
    getVerifyCodeImg () {
      if (this.imgCode) this.imgCode = ''
      getVerifyCode().then(res => {
        this.imgCode = res.Response
      }).catch(() => {

      })
    },
    getBaseInfo () {
      GetBaseInfo().then(res => {
        let data = res.Response
        this.baseInfo = {
          ksmc: '[' + data.ksdm + '] ' + data.ksmc,
          kdmc: '[' + data.kddm + '] ' + data.kdmc,
          tdmc: '[' + data.tddm + '] ' + data.tdmc
        }
      })
    },
    updateServiceUrl() {
      this.serviceFormVisible = true
      this.$nextTick(() => {
        this.$refs.serviceAdrForm.init()
      })
    },
    handleLogin() {
      if (this.loading) return;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store.commit("user/SET_LOGIN_LOADING", true);
          this.$store
            .dispatch("user/login", this.loginForm).then((user) => {
              setLogin(this.loginForm)
              if (user.sfxyxgjkyxx) {
                this.formVisible = true
                this.$nextTick(() => {
                  this.$refs.Form.init()
                })
              } else {
                setIsLogin (true)
                this.$router.push({
                  path: "/home"
                });
              }

            // setHasToken(true)

          })
            .catch(() => {
              this.$store.commit("user/SET_LOGIN_LOADING", false);
            });
        } else {
          return false;
        }
      });
    },
    removeForm(isRefresh) {
      this.formVisible = false
      this.loading = false
      if (isRefresh) {
        this.initData()
      }
    }
  },
};
